﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using PFMCandidate;

namespace PFMHandlers
{
     class PFMFileHandler:PFMHandler
    {
        public override byte[] Perform(Candidate aCandidate, byte[] baTarget)
        {
            base.Perform(aCandidate, baTarget);
            byte[] result = baTarget;
            if (baTarget == null)
                result = OpenFileToByteArray(aCandidate);
            else
                SaveByteArrayToFile(aCandidate, baTarget);
            return result;
        }
        private void SaveByteArrayToFile(Candidate aCandidate, byte[] baTarget)
        {
            using (FileStream fs = new FileStream(aCandidate.Name, FileMode.OpenOrCreate, FileAccess.Write))
            {
               fs.Write(baTarget, 0, (int)baTarget.Length);
            }
        }
        private byte[] OpenFileToByteArray(Candidate aCandidate)
        {
            byte[] result = null;
            using (FileStream fs = new FileStream(aCandidate.Name, FileMode.Open, FileAccess.Read))
            {
                result = new byte[fs.Length];
                fs.Read(result, 0, (int)fs.Length);
            }
            return result;
        }
    }
}
